package com.hfut.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.hfut.dao.UserDao;
import com.hfut.model.Book;
import com.hfut.model.Images;
import com.hfut.model.Message;
import com.hfut.model.RequestBook;
import com.hfut.model.User;

@Component
public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;
	
	@Override
	public boolean register(User u) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.save(u);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public int login(User u) {
		String query = "from User as u where u.username=? and u.password=?";
		Session session = sessionFactory.openSession();
		
		Query q = session.createQuery(query);
		q.setString(0, u.getUsername());
		q.setString(1, u.getPassword());
		
		List<User> list = q.list();
		if(list.size() == 0) {
			session.close();
			return 0;
		}
		session.close();
		return	list.get(0).getId();
	}

	@Override
	public boolean newBook(Book b) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.save(b);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public List<Book> find(String bookName) {
		String query = "from Book as b where b.bookname like '%" + bookName+"%'";
		Session session = sessionFactory.openSession();
		
		Query q = session.createQuery(query);
		List<Book> list = q.list();
		return list;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean deleteBook(Book b) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.delete(b);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public boolean newMessage(Message m) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.save(m);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public boolean newRequest(RequestBook b) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.save(b);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public boolean upImage(Images i) {
		Transaction tran = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			tran = session.beginTransaction();
			session.save(i);
			tran.commit();
		} catch(Exception e) {
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	



}
